home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
minix
/
up1510b.tgz
/
up1510b
/
src
/
commands
/
kermit
/
ckucmd.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-19
|
37KB
|
1,129 lines
char *cmdv = "Unix cmd package V2(022), 27 Jan 88";
/* C K U C M D -- Interactive command package for Unix */
/*
V2 adds support for Data General and Apollo Aegis.
*/
/*
Modelled after the DECSYSTEM-20 command parser (the COMND JSYS)
Features:
. parses and verifies keywords, text strings, numbers, and other data
. displays appropriate menu or help message when user types "?"
. does keyword and filename completion when user types ESC
. accepts any unique abbreviation for a keyword
. allows keywords to have attributes, like "invisible"
. can supply defaults for fields omitted by user
. provides command line editing (character, word, and line deletion)
. accepts input from keyboard, command files, or redirected stdin
. allows for full or half duplex operation, character or line input
. settable prompt, protected from deletion
Functions:
cmsetp - Set prompt (cmprom is prompt string, cmerrp is error msg prefix)
cmsavp - Save current prompt
prompt - Issue prompt
cmini - Clear the command buffer (before parsing a new command)
cmres - Reset command buffer pointers (before reparsing)
cmkey - Parse a keyword
cmnum - Parse a number
cmifi - Parse an input file name
cmofi - Parse an output file name
cmfld - Parse an arbitrary field
cmtxt - Parse a text string
cmcfm - Parse command confirmation (end of line)
stripq - Strip out backslash quotes from a string.
Return codes:
-3: no input provided when required
-2: input was invalid
-1: reparse required (user deleted into a preceding field)
0 or greater: success
See individual functions for greater detail.
Before using these routines, the caller should #include ckucmd.h, and
set the program's prompt by calling cmsetp(). If the file parsing
functions cmifi and cmofi are to be used, this module must be linked
with a ck?fio file system support module for the appropriate system,
e.g. ckufio for Unix. If the caller puts the terminal in
character wakeup ("cbreak") mode with no echo, then these functions will
provide line editing -- character, word, and line deletion, as well as
keyword and filename completion upon ESC and help strings, keyword, or
file menus upon '?'. If the caller puts the terminal into character
wakeup/noecho mode, care should be taken to restore it before exit from
or interruption of the program. If the character wakeup mode is not
set, the system's own line editor may be used.
Author: Frank da Cruz (SY.FDC@CU20B),
Columbia University Center for Computing Activities, January 1985.
Copyright (C) 1985, Trustees of Columbia University in the City of New York.
Permission is granted to any individual or institution to use, copy, or
redistribute this software so long as it is not sold for profit, provided this
copyright notice is retained.
*/
/* Includes */
#include <stdio.h> /* Standard C I/O package */
#include <ctype.h> /* Character types */
#include "ckucmd.h" /* Command parsing definitions */
#include "ckcdeb.h" /* Formats for debug() */
/* Local variables */
int psetf = 0, /* Flag that prompt has been set */
cc = 0, /* Character count */
dpx = 0; /* Duplex (0 = full) */
int hw = HLPLW, /* Help line width */
hc = HLPCW, /* Help line column width */
hh, /* Current help column number */
hx; /* Current help line position */
#define PROML 60 /* Maximum length for prompt */
char cmprom[PROML+1]; /* Program's prompt */
char *dfprom = "Command? "; /* Default prompt */
char cmerrp[PROML+1]; /* Program's error message prefix */
int cmflgs; /* Command flags */
char cmdbuf[CMDBL+4]; /* Command buffer */
char hlpbuf[HLPBL+4]; /* Help string buffer */
char atmbuf[ATMBL+4]; /* Atom buffer */
char filbuf[ATMBL+4]; /* File name buffer */
/* Command buffer pointers */
static char *bp, /* Current command buffer position */
*pp, /* Start of current field */
*np; /* Start of next field */
long zchki(); /* From ck?fio.c. */
/* C M S E T P -- Set the program prompt. */
cmsetp(s) char *s; {
char *sx, *sy, *strncpy();
psetf = 1; /* Flag that prompt has been set. */
strncpy(cmprom,s,PROML - 1); /* Copy the string. */
cmprom[PROML] = NUL; /* Ensure null terminator. */
sx = cmprom; sy = cmerrp; /* Also use as error message prefix. */
while (*sy++ = *sx++) ; /* Copy. */
sy -= 2; if (*sy == '>') *sy = NUL; /* Delete any final '>'. */
}
/* C M S A V P -- Save a copy of the current prompt. */
cmsavp(s,n) int n; char s[]; {
extern char *strncpy(); /* +1 */
strncpy(s,cmprom,n-1);
s[n] = NUL;
}
/* P R O M P T -- Issue the program prompt. */
prompt() {
if (psetf == 0) cmsetp(dfprom); /* If no prompt set, set default. */
printf("\r%s",cmprom); /* Print the prompt. */
}
/* C M R E S -- Reset pointers to beginning of command buffer. */
cmres() {
cc = 0; /* Reset character counter. */
pp = np = bp = cmdbuf; /* Point to command buffer. */
cmflgs = -5; /* Parse not yet started. */
}
/* C M I N I -- Clear the command and atom buffers, reset pointers. */
/*
The argument specifies who is to echo the user's typein --
1 means the cmd package echoes
0 somebody else (system, front end, terminal) echoes
*/
cmini(d) int d; {
for (bp = cmdbuf; bp < cmdbuf+CMDBL; bp++) *bp = NUL;
*atmbuf = NUL;
dpx = d;
cmres();
}
stripq(s) char *s; { /* Function to strip '\' quotes */
char *t;
while (*s) {
if (*s == '\\') {
for (t = s; *t != '\0'; t++) *t = *(t+1);
}
s++;
}
}
/* C M N U M -- Parse a number in the indicated radix */
/* For now, only works for positive numbers in base 10. */
/*
Returns
-3 if no input present when required,
-2 if user typed an illegal number,
-1 if reparse needed,
0 otherwise, with n set to number that was parsed
*/
cmnum(xhlp,xdef,radix,n) char *xhlp, *xdef; int radix, *n; {
int x; char *s;
if (radix != 10) { /* Just do base 10 for now */
printf("cmnum: illegal radix - %d\n",radix);
return(-1);
}
x = cmfld(xhlp,xdef,&s);
debug(F101,"cmnum: cmfld","",x);
if (x < 0) return(x); /* Parse a field */
if (digits(atmbuf)) { /* Convert to number */
*n = atoi(atmbuf);
return(x);
} else {
printf("\n?not a number - %s\n",s);
return(-2);
}
}
/* C M O F I -- Parse the name of an output file */
/*
Depends on the external function zchko(); if zchko() not available, use
cmfld() to parse output file names.
Returns
-3 if no input present when required,
-2 if permission would be denied to create the file,
-1 if reparse needed,
0 or 1 otherwise, with xp pointing to name.
*/
cmofi(xhlp,xdef,xp) char *xhlp, *xdef, **xp; {
int x; char *s;
if (*xhlp == NUL) xhlp = "Output file";
*xp = "";
if ((x = cmfld(xhlp,xdef,&s)) < 0) return(x);
if (chkwld(s)) {
printf("\n?Wildcards not allowed - %s\n",s);
return(-2);
}
if (zchko(s) < 0) {
printf("\n?Write permission denied - %s\n",s);
return(-2);
} else {
*xp = s;
return(x);
}
}
/* C M I F I -- Parse the name of an existing file */
/*
This function depends on the external functions:
zchki() - Check if input file exists and is readable.
zxpand() - Expa